﻿using System;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Mal.WP7.TimeClock.Models;
using Mal.WP7.TimeClock.Repositories;

namespace Mal.WP7.TimeClock.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        #region Fields
        private TimeClockDayViewModel currentDay;
        private DateTime currentDate;

        private RelayCommand beginMorningCommand;
        private RelayCommand endMorningCommand;
        private RelayCommand beginAfternoonCommand;
        private RelayCommand endAfternoonCommand;

        private Repository repository;
        #endregion

        #region Construction / Destruction
        public MainViewModel()
        {
            repository = Repository.Instance;

            TimeClockDay day = repository.Datas.FirstOrDefault(d => d.Day == DateTime.Now.Date);

            if (day != null)
            {
                this.CurrentDay = new TimeClockDayViewModel(day);
            }
            else
            {
                this.CurrentDay = new TimeClockDayViewModel(new TimeClockDay(DateTime.Now.Date));
                repository.Datas.Add(this.CurrentDay.TimeClockDay);
            }

            this.CurrentDate = this.CurrentDay.Day;
        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        public DateTime CurrentDate
        {
            get
            {
                return this.currentDate;
            }

            set
            {
                if (this.currentDate != value)
                {
                    this.currentDate = value;
                    RaisePropertyChanged("CurrentDate");
                }
            }
        }

        public TimeClockDayViewModel CurrentDay
        {
            get
            {
                return this.currentDay;
            }

            set
            {
                if (this.currentDay != value)
                {
                    this.currentDay = value;
                    RaisePropertyChanged("CurrentDay");
                }
            }
        }

        public ICommand EndAfternoonCommand
        {
            get
            {
                if (this.endAfternoonCommand == null)
                {
                    this.endAfternoonCommand = new RelayCommand(this.OnEndAfternoon, this.CanEndAfternoon);
                }

                return this.endAfternoonCommand;
            }
        }

        public ICommand BeginAfternoonCommand
        {
            get
            {
                if (this.beginAfternoonCommand == null)
                {
                    this.beginAfternoonCommand = new RelayCommand(this.OnBeginAfternoon, this.CanBeginAfternoon);
                }

                return this.beginAfternoonCommand;
            }
        }

        public ICommand EndMorningCommand
        {
            get
            {
                if (this.endMorningCommand == null)
                {
                    this.endMorningCommand = new RelayCommand(this.OnEndMorning, this.CanEndMorning);
                }

                return this.endMorningCommand;
            }
        }

        public ICommand BeginMorningCommand
        {
            get
            {
                if (this.beginMorningCommand == null)
                {
                    this.beginMorningCommand = new RelayCommand(this.OnBeginMorning, this.CanBeginMorning);
                }

                return this.beginMorningCommand;
            }
        }
        #endregion

        #region Methods
        private void OnEndAfternoon()
        {
            this.CurrentDay.EndAfternoon = DateTime.Now;
            repository.Save();
        }

        private bool CanEndAfternoon()
        {
            //TODO
            return true;
        }

        private void OnBeginAfternoon()
        {
            this.CurrentDay.BeginAfternoon = DateTime.Now;
            repository.Save();
        }

        private bool CanBeginAfternoon()
        {
            //TODO
            return true;
        }

        private void OnEndMorning()
        {
            this.CurrentDay.EndMorning = DateTime.Now;
            repository.Save();
        }

        private bool CanEndMorning()
        {
            //TODO
            return true;
        }

        private void OnBeginMorning()
        {
            this.CurrentDay.BeginMorning = DateTime.Now;
            repository.Save();
        }

        private bool CanBeginMorning()
        {
            //TODO
            return true;
        }
        #endregion

        #region Events

        #endregion
    }
}
